﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Web;
using System.Web.UI;

namespace Landscape.Elements
{
    internal class Control
    {
        /// <summary>
        /// the name of the control
        /// </summary>
        public string Name;
        /// <summary>
        /// The Virtual path of the control (.ascx)
        /// </summary>
        public string VirtualPath;
        /// <summary>
        /// A dictionary of optional properties to supply
        /// </summary>
        public Dictionary<string, string> Properties;

        public string ControlId;
        public string PlaceHolderId;
        public Dictionary<string, object> Params = new Dictionary<string, object>();

        public Control(XmlReader source) {
            if (source.HasAttributes) {
                while (source.MoveToNextAttribute()) {
                    if (source.Name == "name") {
                        Name = source.Value;
                    } else {
                        if (string.IsNullOrEmpty(source.NamespaceURI)) {
                            Params.Add(source.Name, source.Value);
                        }
                    }
                }
                source.MoveToContent();
            }

            if (string.IsNullOrEmpty(Name)) {
                throw new Exception("Attribute 'name' is missing or empty.");
            }

            VirtualPath = Globals.LookupFile(Name + ".ascx", Context.Instance.controlPaths, true);

            // get configurable properties
            UserControl uc = (UserControl)Renderer.placeholder.Page.LoadControl(VirtualPath);
            Properties = Globals.GetClassProperties(uc.GetType().BaseType, new string[] {
                "System.Int32", "System.Boolean","System.String"
            });

            ControlId = Renderer.placeholder.UniqueID + "_" + Name + Renderer.placeholder.Controls.Count.ToString();
            PlaceHolderId = ControlId + "_pholder";
        }
    }
}
